/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.cruisewatcher;

import com.intellij.openapi.ui.DialogWrapper;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * Created by IntelliJ IDEA. User: rob Date: Aug 17, 2006 Time: 4:08:34 PM To change this template use File | Settings |
 * File Templates.
 */
public class ImageDialog extends DialogWrapper {

    private Icon imageIcon;
    private String buttonName;
    private Runnable buttonAction;
    private Runnable imageAction;

    public ImageDialog(Icon imageIcon, String title, String buttonName, Runnable buttonAction, Runnable imageAction) {
        super(false);
        this.imageIcon = imageIcon;
        this.buttonName = buttonName;
        this.buttonAction = buttonAction;
        this.imageAction = imageAction;
        setTitle(title);
        setButtonsAlignment(0);
        setModal(false);
        init();
    }

    protected Action[] createActions() {
        return new Action[]{
                new ButtonAction(buttonName, buttonAction)
        };
    }

    protected JComponent createCenterPanel() {
        JLabel jLabel = new JLabel(imageIcon);
        jLabel.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent mouseEvent) {
                imageAction.run();
            }
        });
        return jLabel;
    }

    private class ButtonAction extends AbstractAction {

        private Runnable action;

        public ButtonAction(String name, Runnable action) {
            super();
            putValue("Name", name);
            putValue("DefaultAction", Boolean.TRUE);
            this.action = action;
        }

        public void actionPerformed(ActionEvent e) {
            action.run();
            close(0);
        }
    }
}
